class Solution:
    def __init__(self):
        self.dic_pos = {}

    def buildTree(self, inorder: List[int], postorder: List[int]) -> TreeNode:
        for i in range(len(inorder)):
            self.dic_pos[inorder[i]] = i
        return self.dfs(inorder, postorder, 0, len(inorder)-1, 0, len(postorder)-1)

    def dfs(self, inorder, postorder,il,ir,pl,pr):
        if il > ir:
            return None
        root = TreeNode(postorder[pr])
        k = self.dic_pos[root.val]
        root.left = self.dfs(inorder,postorder, il, k-1, pl, pl+k-1-il)
        root.right = self.dfs(inorder,postorder, k+1, ir, pl+k-il, pr-1)
        return root
